/**
 * @Description 
 * @Author LZL
 * @Date 2025/2/25
 */

package com.kcb.login;

import com.kcb.connectDB.connectDB;
import com.kcb.choose.choose;
import com.kcb.usersmanager.usersmanager;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class login extends JFrame {
    private JTextField usernameField;
    private JPasswordField passwordField;
    private JButton loginButton;

    public login() {
        setTitle("城科课程表管理系统:登录功能");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        // 设置窗口图标
        ImageIcon icon = new ImageIcon("C:\\Users\\TBS\\IdeaProjects\\KCB\\image\\ccst.png");
        if (icon != null) {
            setIconImage(icon.getImage());
        } else {
            System.out.println("Failed to load icon image.");
        }

        // 创建主面板
        JPanel panel = new JPanel();
        panel.setBackground(new Color(230, 240, 250)); // 设置背景色
        panel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);
        gbc.fill = GridBagConstraints.HORIZONTAL;

        JLabel titleLabel = new JLabel("欢迎登录");
        titleLabel.setFont(new Font("宋体", Font.BOLD, 22));
        titleLabel.setForeground(new Color(50, 50, 150));

        JLabel usernameLabel = new JLabel("用户名:");
        usernameLabel.setFont(new Font("宋体", Font.PLAIN, 16));

        usernameField = new JTextField(15);
        usernameField.setFont(new Font("宋体", Font.PLAIN, 14));

        JLabel passwordLabel = new JLabel("密码:");
        passwordLabel.setFont(new Font("宋体", Font.PLAIN, 16));

        passwordField = new JPasswordField(15);
        passwordField.setFont(new Font("宋体", Font.PLAIN, 14));
        passwordField.setEchoChar('*');

        loginButton = new JButton("登录");
        loginButton.setFont(new Font("宋体", Font.BOLD, 16));
        loginButton.setBackground(new Color(100, 150, 255));
        loginButton.setForeground(Color.WHITE);
        loginButton.setFocusPainted(false);
        loginButton.setBorderPainted(false);
        loginButton.setPreferredSize(new Dimension(100, 40));

        // 添加组件到布局
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.CENTER;
        panel.add(titleLabel, gbc);

        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.WEST;
        panel.add(usernameLabel, gbc);

        gbc.gridx = 1;
        panel.add(usernameField, gbc);

        gbc.gridx = 0;
        gbc.gridy = 2;
        panel.add(passwordLabel, gbc);

        gbc.gridx = 1;
        panel.add(passwordField, gbc);

        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.CENTER;
        panel.add(loginButton, gbc);

        // 监听器
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = usernameField.getText();
                char[] passwordChars = passwordField.getPassword();
                String password = new String(passwordChars);

                if (validateLogin(username, password)) {
                    if ("admin".equals(username)) {
                        new usersmanager().setVisible(true);
                    } else {
                        new choose(username).setVisible(true);
                    }
                    dispose();
                } else {
                    JOptionPane.showMessageDialog(null, "用户名或密码无效。", "登录失败", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        getContentPane().add(panel);
    }

    private boolean validateLogin(String username, String password) {
        if ("admin".equals(username)) {
            return true;
        }

        connectDB dbConnector = new connectDB();
        return dbConnector.validateLogin(username, password);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new login().setVisible(true);
            }
        });
    }
}
